import axios from "axios";
import { ElNotification } from "element-plus";
import type {AxiosInstance,AxiosError,InternalAxiosRequestConfig,AxiosResponse} from 'axios'

const service:AxiosInstance  = axios.create({
  baseURL:'https://www.demo.com',
  timeout:5000,
})

//配置请求拦截器
service.interceptors.request.use((config:InternalAxiosRequestConfig)=>{
  return config
},(error:AxiosError)=>{
  ElNotification.error({
    title:'Error',
    message:error.message,
    type:'error'
  })
  return Promise.reject(error)
},)

//配置响应拦截器
service.interceptors.response.use((response:AxiosResponse)=>{
  if(response.data.code !=200){
    ElNotification.error({
      title:'Error',
      message:response.data.message,
      type:'error'
    })}
    else{
        return response.data
    }
},

(error:AxiosError)=>{
   ElNotification.error({
    title:'Error',
    message:error.message,
    type:'error'
  })
  return Promise.reject(error)
})

export default service